package com.jinlejia.erp.ui.new_house.house;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.freedom.yefeng.yfrecyclerview.FullyLinearLayoutManager;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.google.android.material.tabs.TabLayout;
import com.jinlejia.erp.R;
import com.jinlejia.erp.ui.new_house.adapter.KeyValueAdapter;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

/**
 * 楼盘信息
 */

public class HouseInfoActivity extends AppCompatActivity {

    private ArrayList<Map<String, String>> dataList = new ArrayList<>();
    private ArrayList<Map<String, String>> quartersDynamicList = new ArrayList<>();
    private ArrayList<Map<String, String>> nearDynamicList = new ArrayList<>();
    private YfListRecyclerView houseDynamic, quartersDynamic, nearDynamic;
    private KeyValueAdapter dynamicAdapter, quartersDynamicAdapter, nearDynamicAdapter;
    private TabLayout tabLayout;
    private LinearLayout housePanLinearLayout, ResidentialQuartersLinearLayout, nearLinearLayout;
    private NestedScrollView nestedScrollView;

    private int height1, height2;
    private boolean IsSlide = false;//默认不是滑动操作
    private boolean isfirst = true;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().registerSticky(this);
        setContentView(R.layout.layout_house_info);
        initView();
    }

    private void initView() {
        ImageView back = (ImageView) findViewById(R.id.id_back);
        houseDynamic = (YfListRecyclerView) findViewById(R.id.id_house_dynamic);
        quartersDynamic = (YfListRecyclerView) findViewById(R.id.id_quarters_dynamic);
        nearDynamic = (YfListRecyclerView) findViewById(R.id.id_near_dynamic);
        tabLayout = (TabLayout) findViewById(R.id.id_table_layout);
        housePanLinearLayout = (LinearLayout) findViewById(R.id.housePanLinearLayout);
        ResidentialQuartersLinearLayout = (LinearLayout) findViewById(R.id.ResidentialQuartersLinearLayout);
        nearLinearLayout = (LinearLayout) findViewById(R.id.nearLinearLayout);
        nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tabLayout.addTab(tabLayout.newTab().setText("基本信息"));
        tabLayout.addTab(tabLayout.newTab().setText("小区信息"));
        tabLayout.addTab(tabLayout.newTab().setText("周边信息"));

        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //第二个参数:目前的x轴坐标
                //第三个参数:目前的y轴坐标
                //第四个参数:上一个x轴坐标
                //第五个参数:上一个y轴坐标
                if (isfirst) {
                    getHight();
                    isfirst = false;
                }
                IsSlide = true;//表明此时正在滑动
                View contentView = v.getChildAt(0);
                if (scrollY < height1) {//表明此时在Item1范围内，所以第一个Item应该被选择
                    Objects.requireNonNull(tabLayout.getTabAt(0)).select();
                } else if (contentView.getMeasuredHeight() == v.getScrollY() + v.getHeight()) {//互动到底部第三个Item应该被选择
                    Objects.requireNonNull(tabLayout.getTabAt(2)).select();
                } else if (scrollY > height1 && scrollY < height2) {//表明此时在Item2范围内，所以第二个Item应该被选择
                    Objects.requireNonNull(tabLayout.getTabAt(1)).select();
                }
                IsSlide = false;//表明滑动结束
            }
        });
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //当标签处于选中的状此时有两种情况
                // 1.直接点击tabLayout标签会走
                // 2.当ScrollView滑动时，因为我们在滑动里面设置了选择标签，所以此时这个方法也会走
                //现在有个问题，当滑动的时候，tabLayout标签也会被选择，这个方法也会走，所以此时会产生冲突，我们要排除滑动的操作
                if (IsSlide) {//表示此时正在滑动  不走这个方法
                    return;
                }
                if (isfirst) {
                    getHight();
                    isfirst = false;
                }
                int position = tab.getPosition();//表明哪个标签被选择
                if (position == 0) {
                    nestedScrollView.smoothScrollTo(0, 0);//滑到对应的View顶部
                } else if (position == 1) {
                    nestedScrollView.smoothScrollTo(0, height1 + 10);//滑到对应的View顶部
                } else if (position == 2) {
                    nestedScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);//滚动到底部
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        //region 基本信息
        houseDynamic.setHasFixedSize(true);
        houseDynamic.setLayoutManager(new FullyLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        houseDynamic.setNestedScrollingEnabled(false);
        dynamicAdapter = new KeyValueAdapter(dataList);
        houseDynamic.setAdapter(dynamicAdapter);
        //endregion
        //region 小区信息
        quartersDynamic.setHasFixedSize(true);
        quartersDynamic.setLayoutManager(new FullyLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        quartersDynamic.setNestedScrollingEnabled(false);
        quartersDynamicAdapter = new KeyValueAdapter(quartersDynamicList);
        quartersDynamic.setAdapter(quartersDynamicAdapter);
        //endregion
        //region 周边信息
        nearDynamic.setHasFixedSize(true);
        nearDynamic.setLayoutManager(new FullyLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        nearDynamic.setNestedScrollingEnabled(false);
        nearDynamicAdapter = new KeyValueAdapter(nearDynamicList);
        nearDynamic.setAdapter(nearDynamicAdapter);
        //endregion

    }

    @Subscriber(tag = "TAG_HOUSE_INFO")
    public void getData(ArrayList<Map<String, String>> dataList) {
        EventBus.getDefault().removeStickyEvent(dataList.getClass(), "TAG_HOUSE_INFO");
        dynamicAdapter.setData(dataList);
        dynamicAdapter.notifyDataSetChanged();
    }

    @Subscriber(tag = "TAG_QUQRTERS_INFO")
    public void getQuartersData(ArrayList<Map<String, String>> dataList) {
        EventBus.getDefault().removeStickyEvent(dataList.getClass(), "TAG_QUQRTERS_INFO");
        quartersDynamicAdapter.setData(dataList);
        quartersDynamicAdapter.notifyDataSetChanged();
    }

    @Subscriber(tag = "TAG_NEAR_INFO")
    public void getNearData(ArrayList<Map<String, String>> dataList) {
        EventBus.getDefault().removeStickyEvent(dataList.getClass(), "TAG_NEAR_INFO");
        nearDynamicAdapter.setData(dataList);
        nearDynamicAdapter.notifyDataSetChanged();
    }

    private void getHight() {
        height1 = housePanLinearLayout.getMeasuredHeight();//表示第一个item的高度,这是高度
        height2 = housePanLinearLayout.getMeasuredHeight() + ResidentialQuartersLinearLayout.getMeasuredHeight();//这个是第一个item加第二个item的高度  下面同理
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
